package com.wanshi.dao;

import com.wanshi.entity.CartItem;
import com.wanshi.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CartDao {

    //1 查询  userid  prodid 是否存在
    public CartItem findCartByUserAndProd(String userid,String prodid)  {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "SELECT * FROM cartitem WHERE user_id = ? AND prod_id = ?";

        try {
            return runner.query(sql,new BeanHandler<>(CartItem.class),userid,prodid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //2 修改   userid  prodid   原来的数量 + 1
    public void updateCartNum(String cartid){
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "update cartitem set buynum=buynum+1 where id = ?";
        try {
            runner.update(sql,cartid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //3 添加  insert  userid  prodid  1
    public void addCart(String userid,String prodid){
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "insert into cartitem values(null,?,?,1)";
        try {
            runner.update(sql,userid,prodid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<CartItem> findCartItemByUserId(String userid) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "SELECT * FROM cartitem WHERE user_id = ?";

        try {
            return runner.query(sql,new BeanListHandler<>(CartItem.class),userid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void chageCartNum(String userid, String prodid, String num) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "update cartitem set buynum = ? where user_id =? and prod_id = ?";
        try {
            runner.update(sql,num,userid,prodid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delCartById(String userid, String prodid) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "delete from cartitem where user_id =? and prod_id = ?";
        try {
            runner.update(sql,userid,prodid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void clearCart(Connection conn,String userid) {
        QueryRunner runner = new QueryRunner();
        String sql = "delete from cartitem where user_id =?";
        try {
            runner.update(conn,sql,userid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void clearCart(String userid) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "delete from cartitem where user_id =?";
        try {
            runner.update(sql,userid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
